// const yuMing = {
//   host: 'http://localhost:3000',
//   mobileHost: 'https://zhiyongzaixian123123.utools.club'
// }
const yuMing = {
  // 网上淘的接口
  host: 'https://www.codeman.store',
  // 内网穿透
  mobileHost: 'https://zaizai-wx-demo.cn.utools.club',
  // 本地
  bendi: 'http://localhost:3000'
}

const http = {
  get (url, data = {}) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: yuMing.bendi + url,
        data,
        method: 'GET',
        header: {
          cookie: wx.getStorageSync('cookies') ? wx.getStorageSync('cookies').find((item) => item.indexOf('MUSIC_U') !== -1) : '',
        },
        success: (res) => {
          if (data.isLogin) {
            wx.setStorage({
              key: 'cookies',
              data: res.cookies,
            })
          }
          resolve(res.data)
        },
        fail: (err) => {
          console.error('网络请求错误', err)
          this.showTltie()
          reject(err)
        },
      })
    })
  },
  post (url, data = {}) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: yuMing.mobileHost + url,
        data,
        header: {
          cookie: wx.getStorageSync('cookies') ? wx.getStorageSync('cookies').find((item) => item.indexOf('MUSIC_U') !== -1) : '',
        },
        method: 'POST',
        success: (res) => {
          resolve(res.data)
        },
        fail: (err) => {
          console.error('网络请求错误', err)
          this.showTltie()
          reject(err)
        },
      })
    })
  },
  showTltie () {
    wx.showToast({
      title: '网络错误',
      icon: 'error',
      duration: 2000,
      mask: true,
    });
  }
}
export default http
